﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Data;

namespace PRO.Controls
{
    /// <summary>
    /// 删除控件-只能用户GridView控件
    /// </summary>
    [ToolboxData("<{0}:DelField runat=server></{0}:DelField>")]
    public class DelField : BoundField
    {
        #region 属性
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue(true),
        Description("是否弹出删除提示对话框"),
        Localizable(true)]
        public bool DelConfirm
        {
            get
            {
                if (ViewState["DelConfirm"] == null || ViewState["DelConfirm"].ToString() == "")
                    return true;
                return Convert.ToBoolean(ViewState["DelConfirm"]);
            }
            set
            {
                ViewState["DelConfirm"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue("您确定要删除吗？"),
        Description("删除提示框的提示内容"),
        Localizable(true)]
        public string AlertText
        {
            get
            {
                if (ViewState["AlertText"] == null || ViewState["AlertText"].ToString() == "")
                    return "您确定要删除吗？";
                return (String)ViewState["AlertText"];
            }
            set
            {
                ViewState["AlertText"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue("删除"),
        Description("按钮文字"),
        Localizable(true)]
        public string Text
        {
            get
            {
                if (ViewState["Text"] == null || ViewState["Text"].ToString() == "")
                    return "删除";
                return (String)ViewState["Text"];
            }
            set
            {
                ViewState["Text"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue(""),
        Description("图片路径"),
        Localizable(true)]
        public string Icon
        {
            get
            {
                return (String)ViewState["Icon"];
            }
            set
            {
                ViewState["Icon"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue(""),
        Description("操作的表名"),
        Localizable(true)]
        public string TableName
        {
            get
            {
                return (String)ViewState["Del_TableName"];
            }
            set
            {
                ViewState["Del_TableName"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue(""),
        Description("操作表的主键列名"),
        Localizable(true)]
        public string KeyField
        {
            get
            {
                if (ViewState["Del_KeyField"] == null || (String)ViewState["Del_KeyField"] == "")
                    return "id";
                return (String)ViewState["Del_KeyField"];
            }
            set
            {
                ViewState["Del_KeyField"] = value.ToString();
            }
        }
        [CategoryAttribute("删除设置"),
        Bindable(true),
        DefaultValue(""),
        Description("排序时的过滤条件（sql语句），例如：type=5"),
        Localizable(true)]
        public string Filter
        {
            get
            {
                return (String)ViewState["Del_Filter"];
            }
            set
            {
                ViewState["Del_Filter"] = value.ToString();
            }
        }


        [CategoryAttribute("设置删除按钮样式"),
        Bindable(true),
        DefaultValue(DelStyleItems.Button),
        Description("设置删除按钮样式"),
        Localizable(true)]
        public DelStyleItems DelBtnStyle
        {
            get
            {
                if (ViewState["DelBtnStyle"] == null || ViewState["DelBtnStyle"].ToString() == "")
                    return DelStyleItems.Button;
                return (DelStyleItems)ViewState["DelBtnStyle"];
            }
            set
            {
                ViewState["DelBtnStyle"] = value;
            }
        }
        public enum DelStyleItems
        {
            Button = 0,
            Link = 1,
            Image = 2,
        }
        #endregion

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            switch (this.DelBtnStyle.ToString())
            {
                case "Button":
                    Button del_btn = new Button();
                    del_btn.Text = this.Text;
                    del_btn.Click += Del_Btn_Click;
                    del_btn.ID = "Del_Btn_BoundField";
                    if (this.DelConfirm)
                        del_btn.OnClientClick = "javascript:return confirm('" + this.AlertText + "');";
                    cell.Controls.Add(del_btn);
                    break;
                case "Link":
                    LinkButton del_link = new LinkButton();
                    del_link.ID = "Del_Link_BoundField";
                    del_link.Text = this.Text;
                    del_link.Click += Del_Link_Click;
                    if (this.DelConfirm)
                        del_link.OnClientClick = "javascript:return confirm('" + this.AlertText + "');";
                    cell.Controls.Add(del_link);
                    break;
                case "Image":
                    ImageButton del_image = new ImageButton();
                    del_image.ImageUrl = this.Icon;
                    del_image.ToolTip = this.Text;
                    del_image.Click+=Del_Image_Click;
                    if (this.DelConfirm)
                        del_image.OnClientClick = "javascript:return confirm('" + this.AlertText + "');";
                    cell.Controls.Add(del_image);
                    break;
            }

            base.InitializeCell(cell, cellType, rowState, rowIndex);
        }

        

        void Del_Btn_Click(object sender, EventArgs e)
        {
            Button cell = sender as Button;
            GridViewRow container = (GridViewRow)cell.Parent.NamingContainer;
            GridView gridview = (GridView)cell.Parent.Parent.NamingContainer;
            int key = Convert.ToInt32(gridview.DataKeys[container.RowIndex].Value);
            if (DelFieldManager.Del(this.TableName, this.KeyField, key, this.Filter))
            {
                DelCallBack_Event(key);
            }
        }
        void Del_Link_Click(object sender, EventArgs e)
        {
            LinkButton cell = sender as LinkButton;
            GridViewRow container = (GridViewRow)cell.Parent.NamingContainer;
            GridView gridview = (GridView)cell.Parent.Parent.NamingContainer;
            int key = Convert.ToInt32(gridview.DataKeys[container.RowIndex].Value);
            if (DelFieldManager.Del(this.TableName, this.KeyField, key, this.Filter))
            {
                DelCallBack_Event(key);
            }
        }
        void Del_Image_Click(object sender, EventArgs e)
        {
            ImageButton cell = sender as ImageButton;
            GridViewRow container = (GridViewRow)cell.Parent.NamingContainer;
            GridView gridview = (GridView)cell.Parent.Parent.NamingContainer;
            int key = Convert.ToInt32(gridview.DataKeys[container.RowIndex].Value);
            if (DelFieldManager.Del(this.TableName, this.KeyField, key, this.Filter))
            {
                DelCallBack_Event(key);
            }
        }

        #region 自定义事件
        //定义委托
        public delegate void DelCallBackHandle(int keyvalue);
        //定义事件
        public event DelCallBackHandle DelCallBack;
        /// <summary>
        /// 删除回调
        /// </summary>
        /// <param name="keyvalue">当前删除的主键值</param>
        private void DelCallBack_Event(int keyvalue)
        {
            if (DelCallBack != null)
                DelCallBack(keyvalue);//自身作为参数传递
        }
        #endregion
    }
    public class DelFieldManager
    {
        public static bool Del(string table, string key, int id, string filter)
        {
            bool r = false;
            if (filter != null && filter.ToString() != "")
                filter = " and " + filter + " ";
            string sql = "delete " + table + " where " + key + "=" + id + filter;
            r = Convert.ToBoolean(DbHelper.ExecuteNonQuery(CommandType.Text, sql));
            return r;
        }
    }
}
